package com.waya.web.browser.webview;

import android.app.Dialog;
import android.content.Context;
import android.os.Looper;
import android.util.ArrayMap;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.waya.web.browser.config.WebConfig;
import com.waya.web.browser.event.EventHandlerImp;
import com.waya.web.browser.js_interface.JSInterfaceImp;
import com.waya.web.browser.player.view.MediaPlayerLayout;
import com.waye.wanba.utils.APPLog;

import java.util.Map;
import java.util.WeakHashMap;

/**
 * Created by JesseHev on 2017/12/4.
 * describe:
 */

public class WebBuilder {

    private Context context;

    private JSInterfaceImp jsInterfaceImp;

    private WebView webView;

    private WanbaEpgInterface.Callback callback;

    private WebSettings setting;

    private String url;

    private WebViewClient client;

    private WebChromeClient chromeClicent;

    private EventHandlerImp handlerImp;

    private Map<String, Object> mObjectMap;

    private AndroidContext jsContext;

    private AndroidBrowser jsBrowser;

    private AndroidMediaplayer jsPlayer;

    private MediaPlayerLayout mediaPlayerLayout;

    private Dialog dialog;

    public WebBuilder(Context context) {
        this.context = context;
    }

    public WebBuilder target(WebView webView) {
        this.webView = webView;
        return this;
    }

    public WebBuilder client(WebViewClient client) {
        this.client = client;
        return this;
    }

    public WebBuilder chromeClicent(WebChromeClient chromeClicent) {
        this.chromeClicent = chromeClicent;
        return this;
    }

    public WebBuilder handlerEvent(EventHandlerImp handlerImp) {
        this.handlerImp = handlerImp;
        return this;
    }

    public WebBuilder setting(WebSettings setting) {
        this.setting = setting;
        return this;
    }

    public WebBuilder setCallback(WanbaEpgInterface.Callback callback) {
        this.callback = callback;
        return this;
    }

    public WebBuilder playerLayout(MediaPlayerLayout layout) {
        mediaPlayerLayout = layout;
        return this;
    }

    public WebBuilder dialog(Dialog dialog) {
        this.dialog = dialog;
        return this;
    }

    public void registerJsObj() {
        this.mObjectMap = new WeakHashMap<>();

        jsContext = new AndroidContext(context);
        jsBrowser = new AndroidBrowser(context, webView);
        if (mediaPlayerLayout == null) {
            APPLog.printWarn("as not yet been registered mediaplayer interface");
        } else {
            jsPlayer = new AndroidMediaplayer(context, webView, mediaPlayerLayout);
            mObjectMap.put(WebConfig.OBJ_NAME_MEDIAP_LAYER, jsPlayer);
        }
        mObjectMap.put(WebConfig.OBJ_NAME_BROWSER, jsBrowser);
        mObjectMap.put(WebConfig.OBJ_NAME_CONTEXT, jsContext);
    }

    public EventHandlerImp getHandlerImp() {
        return handlerImp;
    }

    public WebSettings getSetting() {
        return setting;
    }

    public WebViewClient getClient() {
        return client;
    }

    public String getUrl() {
        return url;
    }

    public WebView getWebView() {
        return webView;
    }

    public WebBuilder load(String url) {
        this.url = url;
        return this;
    }

    public void build() {
        if (webView == null) {
            throw new NullPointerException("please invoke target to set webview...");
        }
        if (mObjectMap == null) {
            throw new IllegalArgumentException("please invoke registerObj to addJavaScripObj...");
        }
        jsInterfaceImp = new JSInterfaceImp(webView);
        jsInterfaceImp.addJavaObjects(mObjectMap);
        webView.setWebViewClient(client);
        webView.setWebChromeClient(chromeClicent);
        webView.loadUrl(url);
    }

    public void destroy() {
        if (webView == null) {
            return;
        }
        webView.destroy();
//        webView.clearHistory();
//        ((ViewGroup) webView.getParent()).removeView(webView);
//
//        if (Looper.myLooper() != Looper.getMainLooper()) {
//            return;
//        }
//        webView.loadUrl("about:blank");
//        webView.stopLoading();
//        if (webView.getHandler() != null) {
//            webView.getHandler().removeCallbacksAndMessages(null);
//        }
//        webView.removeAllViews();
//        ViewGroup mViewGroup = null;
//        if ((mViewGroup = ((ViewGroup) webView.getParent())) != null) {
//            mViewGroup.removeView(webView);
//        }
//        webView.setWebChromeClient(null);
//        webView.setWebViewClient(null);
//        webView.setTag(null);
//        webView.clearHistory();
//        webView.destroy();
//        webView = null;
//        if (mediaPlayerLayout == null) {
//            return;
//        }
//        mediaPlayerLayout.onDestroy();

    }
}
